package org.etherframework.tests.features.mediator
{
	import org.etherframework.common.IContractual;
	import org.etherframework.core.IModule;
	import org.etherframework.mediator.IMediators;
	import org.etherframework.tests.features.mediator.mediators.AutomaticallyCreatedViewMediator;
	import org.etherframework.tests.features.mediator.views.AutomaticallyCreatedView;
	import org.etherframework.tests.utils.ModuleInfo;
	import org.etherframework.tests.utils.TestUtility;
	import org.fluint.uiImpersonation.UIImpersonator;
	import org.hamcrest.assertThat;
	import org.hamcrest.object.isTrue;
	import org.hamcrest.object.nullValue;
	
	import spark.components.Group;

	public class when_removing_view
	{	
		private static var _testUtility:TestUtility = new TestUtility();
		private static var _module:IModule;
		private static var _automaticallyCreatedView:AutomaticallyCreatedView;
		private static var _automaticallyCreatedViewMediator:AutomaticallyCreatedViewMediator;
		private static var _m:IMediators;
		private static var _moduleView:Group;
		private static var _info:ModuleInfo;
		
		[BeforeClass]
		public static function setUpBeforeClass():void
		{
			_moduleView = UIImpersonator.addChild(new Group()) as Group;
			_info = _testUtility.createModuleInfo(null, _moduleView);
			_module = _info.module;
			_m = _module.mediators;
			
			_m.addMediator(AutomaticallyCreatedView, AutomaticallyCreatedViewMediator, IContractual);
			
			_automaticallyCreatedView = new AutomaticallyCreatedView();
			_moduleView.addElement(_automaticallyCreatedView);
			_automaticallyCreatedViewMediator = _m.hasMediator(_automaticallyCreatedView) as AutomaticallyCreatedViewMediator;
			
			_moduleView.removeElement(_automaticallyCreatedView);
		}
		
		[AfterClass]
		public static function tearDownAfterClass():void
		{
			_testUtility.dispose();
			UIImpersonator.removeAllChildren();
		}
		
		[Test(order=1)]
		public function mediator_should_be_destroyed():void
		{
			assertThat(_info.controllers.hasController(_automaticallyCreatedViewMediator), nullValue());
			assertThat(_m.hasMediator(_automaticallyCreatedView), nullValue());
			assertThat(_automaticallyCreatedViewMediator.destroyed, isTrue());
		}
	}
}